package com.baiye.template.service.impl;

/**
 * @author GUOZHIPENG
 * @version 1.0
 * @date 2021/8/29 16:58
 */

import com.baiye.template.dao.LogDao;
import com.baiye.template.pojo.ExceptionLog;
import com.baiye.template.pojo.OperationLog;
import com.baiye.template.service.LogService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LogServiceImpl implements LogService {
    @Autowired
    private LogDao logDao;
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public int saveExceptionLog(ExceptionLog excepLog) {
        return 0;
    }

    @Override
    public int saveOperLog(OperationLog operlog) {
        //operlog.setReqParam(operlog.getReqParam().replace('\"', '\''));
        //operlog.setRespParam(operlog.getRespParam().replace('\"', '\''));
        mongoTemplate.save(operlog);
        return logDao.saveOperLog(operlog);
    }

    @Override
    public List<OperationLog> getOperLog(OperationLog log, Integer pageIndex, Integer pageSize) {
        int start = (pageIndex-1)*pageSize;
        PageHelper.startPage(pageIndex,pageSize);
        return logDao.getOperLog(log,start,pageSize);
    }

    @Override
    public List<ExceptionLog> getExceptionLog(ExceptionLog log, Integer pageIndex, Integer pageSize) {
        int start = (pageIndex-1)*pageSize;
        PageHelper.startPage(pageIndex,pageSize);
        return logDao.getExceptionLog(log,start,pageSize);
    }

    @Override
    public int deleteLog(List idList, String tableName) {
        if(tableName.equals("oper_log")||tableName.equals("exception_log")){
            return logDao.deleteLog(idList,tableName);
        }
        return 0;
    }

}
